﻿@page "/"
@page "/app"
@using RatTracking.FetchModels
@using System.Diagnostics
@inject Interfaces.IRatScannerUI RatScannerUI
@inject RatTracking.TarkovTrackerDB TarkovTrackerDB
@inject IJSRuntime JSRuntime;
@implements IDisposable

<MudGrid Class="pa-0" Spacing="0">
	<MudItem xs="12">
		<div class="d-flex align-center justify-start mx-2 mt-2 mb-1">
			<img src="@RatScannerUI.IconLink" class="mr-2 mud-elevation-1" style="border-style: solid; border-width: 1px; border-color: #585D60; display: block; max-width: 64px; max-height: 64px; width: auto; height: auto;"/>
			<div style="text-shadow: 2px 2px 5px black;">
				<MudText Typo="Typo.subtitle1" Color="Color.Inherit">
					@(RatScannerUI.Name[0..Math.Min(RatScannerUI.Name.Length, 60)] + (RatScannerUI.Name.Length > 60 ? "..." : ""))
				</MudText>
			</div>
		</div>
	</MudItem>
	<MudItem xs="6">
		<MudPaper Class="d-flex align-center justify-center pa-1 ml-2 mr-1 my-1 mud-grey-darken4" Elevation="2" @onclick="OpenWiki" Style="cursor: pointer;">
			<img src="https://static.wikia.nocookie.net/escapefromtarkov_gamepedia/images/e/e6/Site-logo.png" height="24px"/>
		</MudPaper>
	</MudItem>
	<MudItem xs="6">
		<MudPaper Class="d-flex align-center justify-center pa-1 ml-1 mr-2 my-1" Elevation="2" @onclick="OpenTarkovTools" Style="cursor: pointer;">
			<img src="https://tarkovtracker.io/img/tarkovtoolslogo.png" height="24px"/><MudText Typo="Typo.subtitle2" Style="@($"color:{Colors.Amber.Lighten5};")">Tarkov Tools</MudText>
		</MudPaper>
	</MudItem>
	<MudItem xs="12">
		<MudPaper Class="align-center justify-center pa-2 mx-2 my-1" Elevation="2">
			<div class="d-flex justify-space-between align-center mt-2">
				<MudText Typo="Typo.subtitle2">Recent Avg. Price</MudText>
				<MudText Typo="Typo.subtitle2">@RatScannerUI.IntToLongPrice(RatScannerUI.Avg24hPrice)</MudText>
			</div>
			<div class="d-flex justify-space-between align-center my-1">
				<MudText Typo="Typo.subtitle2">Value per Slot</MudText>
				<MudText Typo="Typo.subtitle2">@RatScannerUI.IntToLongPrice(RatScannerUI.PricePerSlot)</MudText>
			</div>
			<MudDivider DividerType="DividerType.FullWidth"/>
			<div class="d-flex justify-space-between align-center mt-2">
				<MudText Typo="Typo.subtitle2">Best Trader</MudText>
				<div class="d-flex align-center">
					<MudAvatar Image="@($"https://tarkovtracker.io/img/{@RatScannerUI.TraderName}Headshot.jpg")" Size="Size.Small" Style="float: left;" Class="mr-1" Elevation="1"/><MudText Typo="Typo.subtitle2" Style="float: right;">@RatScannerUI.TraderName</MudText>
				</div>
			</div>
			<div class="d-flex justify-space-between align-center my-1">
				<MudText Typo="Typo.subtitle2">Trader Price</MudText>
				<MudText Typo="Typo.subtitle2">@RatScannerUI.IntToLongPrice(RatScannerUI.BestTraderPrice)</MudText>
			</div>
		</MudPaper>
	</MudItem>
	<MudItem xs="12">
		<MudPaper Class="d-flex align-center justify-space-around pa-1 mx-2 my-1" Elevation="2">
			@*<div class="d-flex align-center">
                        @RatScannerUI.TrackingNeedsQuestRemaining <MudIcon Icon="@Icons.Filled.Checklist" Size="Size.Small" Class="ml-1" /> Quest
                    </div>
                    <div class="d-flex align-center">
                        @RatScannerUI.TrackingNeedsHideoutRemaining <MudIcon Icon="@Icons.Filled.House" Size="Size.Small" Class="ml-1" /> Hideout
                    </div>*@
			@if (RatScannerUI.TrackingTeamNeeds != null && (RatScannerUI.TrackingTeamNeedsSummed.QuestRemaining > 0 || RatScannerUI.TrackingTeamNeedsSummed.HideoutRemaining > 0))
			{
				<MudGrid Spacing="0" Class="pa-0 ma-0">
					@foreach (var teammate in RatScannerUI.TrackingTeamNeeds.Where(t => t.Value.QuestRemaining > 0 || t.Value.HideoutRemaining > 0).ToList())
					{
						<MudItem xs="12" Class="pa-0 ma-0">
							<div class="d-flex justify-space-between">
								<div class="mx-1">@teammate.Key</div>
								<div class="d-flex align-center">
									<MudIcon Icon="@Icons.Filled.Checklist" Size="Size.Small" Class="mr-1"/> @teammate.Value.QuestRemaining
									<MudIcon Icon="@Icons.Filled.House" Size="Size.Small" Class="ml-3 mr-1"/> @teammate.Value.HideoutRemaining
								</div>
							</div>
						</MudItem>
					}
				</MudGrid>
			}
			else
			{
				<div class="d-flex align-center">
					@RatScannerUI.TrackingNeeds.QuestRemaining <MudIcon Icon="@Icons.Filled.Checklist" Size="Size.Small" Class="ml-1"/> Quest
				</div>
				<div class="d-flex align-center">
					@RatScannerUI.TrackingNeeds.HideoutRemaining <MudIcon Icon="@Icons.Filled.House" Size="Size.Small" Class="ml-1"/> Hideout
				</div>
			}
		</MudPaper>
	</MudItem>
</MudGrid>

@code {

	private async void OpenWiki()
	{
		OpenURL(RatScannerUI.WikiLink);
	}

	private async void OpenTarkovTools()
	{
		OpenURL(RatScannerUI.ToolsLink);
	}

	private void OpenURL(string url)
	{
		var psi = new ProcessStartInfo
		{
			FileName = url,
			UseShellExecute = true,
		};
		Process.Start(psi);
	}

	protected override void OnInitialized()
	{
		RatScannerUI.PropertyChanged += PropertyChangeHandler;
	}

	private async void PropertyChangeHandler(object sender, EventArgs e)
	{
		await InvokeAsync(() => StateHasChanged());
	}

	public void Dispose()
	{
		RatScannerUI.PropertyChanged -= PropertyChangeHandler;
	}

}
